package cn.mengfly.fxtest.app;/**
 * @date 2021/5/28 15:14
 * @author Mengfly
 */

import cn.mengfly.fxtest.util.UiUtil;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import lombok.AllArgsConstructor;
import lombok.Data;

public class VboxAndHboxApplication extends Application {



    @Data
    @AllArgsConstructor
    public static class Customer {
        private String firstName;
        private String lastName;
    }

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();

        HBox topControls = new HBox();
        topControls.setAlignment(Pos.BOTTOM_LEFT);

        Button btnRefresh = new Button("Refresh");
        Hyperlink signOutLink = new Hyperlink("Sign out");

        HBox topRightControls = new HBox();
        HBox.setHgrow(topRightControls, Priority.ALWAYS);
        topRightControls.setAlignment(Pos.BOTTOM_RIGHT);
        topRightControls.getChildren().add(signOutLink);
        topControls.getChildren().addAll(btnRefresh, topRightControls);

        TableView<Customer> tblCustomer = new TableView<>();
        VBox.setVgrow(tblCustomer, Priority.ALWAYS);
        tblCustomer.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        UiUtil.initTable(tblCustomer, Customer.class);
        loadTable(tblCustomer);
        Separator sep = new Separator();

        Button btnClose = new Button("Close");
        HBox bottomControls = new HBox();
        bottomControls.setAlignment(Pos.BOTTOM_RIGHT);
        bottomControls.getChildren().add(btnClose);

        VBox.setMargin(topControls, new Insets(10d));
        VBox.setMargin(tblCustomer, new Insets(0.d, 10.d, 10.d, 10.d));
        VBox.setMargin(bottomControls, new Insets(10d));
        root.getChildren().addAll(topControls, tblCustomer, sep, bottomControls);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Vbox And Hbox Application");
        primaryStage.show();
    }

    private void loadTable(TableView<Customer> tblCustomers) {
        tblCustomers.getItems().add(new Customer("George", "Washington"));
        tblCustomers.getItems().add(new Customer("Abe", "Lincoln"));
        tblCustomers.getItems().add(new Customer("Thomas", "Jefferson"));
    }
}
